<template>
    <view class="login" :style="{ direction: $store.state.dir }">
        <input-item :name="$t('login.code')" icon="icon-user">
            <input v-model="code" class="input" type="text" />
        </input-item>
        <input-item :name="$t('login.password')" icon="icon-user">
            <input v-model="password" class="input" type="text" />
        </input-item>
        <view class="login-button">
            <u-button @click="handleReset" type="primary" size="large" shape="circle" :text="$t('login.loginBtn')"></u-button>
        </view>
    </view>
</template>

<script>
import { APP_ROLE, CurrentRole } from '../../config';
import { showFailure, showMessage, showSuccess } from '../../lib/helper';
import { login } from '../../lib/service';
export default {
    data() {
        return {
            email: '',
            code: '',
            isRotate: false //是否加载旋转
        };
    },
    mounted() {},
    methods: {
        handleReset() {
            if (this.isRotate) {
                //判断是否加载中，避免重复点击请求
                return false;
            }

            if (!emailRegex.test(this.email)) {
                showAlert(this.$t('message.email.wrong'));
                return false;
            }

            uni.showLoading();
            forget({
                email: this.email
            })
                .then((res) => {
                    uni.hideLoading();
                    showSuccess();

                    uni.switchTab({
                        url: '/pages/user/user'
                    });
                })
                .catch((err) => {
                    uni.hideLoading();
                    showFailure();
                });
        },
        handleRegButton1() {
            uni.navigateTo({
                url: '/pages/login/register?type=1'
            });
        },
        handleRegButton2() {
            uni.navigateTo({
                url: '/pages/login/register?type=2'
            });
        },
        handleRegButton3() {
            uni.navigateTo({
                url: '/pages/login/register?type=' + (CurrentRole == APP_ROLE.Agent ? '2' : '1')
            });
        }
    }
};
</script>

<style lang="scss" scoped>
.login {
    padding: 0 30px;

    .login-button {
        margin-top: 20px;
    }
}
</style>
